package com.hi.cdi.versioning.repository;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

import com.hi.cdi.versioning.domain.Record;
import com.hi.cdi.versioning.domain.Record_History;
import com.mongodb.WriteResult;

@Component
public class RecordDao {

	@Autowired
	private MongoTemplate mongoTemplate;

	public void save(Record record) {
		mongoTemplate.insert(Record_History.prepareNewHistoryRecord(record));
		mongoTemplate.insert(record);
	}

	public void update(Record record) {
		Query query = Query.query(Criteria.where("facebookName").is(
				record.getFacebookName()));
		Update update = new Update();
		if (StringUtils.isNotBlank(record.getCountrtCode())) {
			update.set("countrtCode", record.getCountrtCode());
		}
		if (record.getSomeId1() != 0L) {
			update.set("someId1", record.getSomeId1());
		}
		if (StringUtils.isNotBlank(record.getStr1())) {
			update.set("str1", record.getStr1());
		}
		if (StringUtils.isNotBlank(record.getStr2())) {
			update.set("str2", record.getStr2());
		}
		if (StringUtils.isNotBlank(record.getStr3())) {
			update.set("str3", record.getStr3());
		}
		if (StringUtils.isNotBlank(record.getStr4())) {
			update.set("str4", record.getStr4());
		}
		if (StringUtils.isNotBlank(record.getStr5())) {
			update.set("str5", record.getStr5());
		}
		if (StringUtils.isNotBlank(record.getStr6())) {
			update.set("str6", record.getStr6());
		}
		if (StringUtils.isNotBlank(record.getStr7())) {
			update.set("str7", record.getStr7());
		}
		if (StringUtils.isNotBlank(record.getStr8())) {
			update.set("str8", record.getStr8());
		}
		if (StringUtils.isNotBlank(record.getStr9())) {
			update.set("str9", record.getStr9());
		}
		if (StringUtils.isNotBlank(record.getStr10())) {
			update.set("str10", record.getStr10());
		}
		if (StringUtils.isNotBlank(record.getStr11())) {
			update.set("str11", record.getStr11());
		}
		if (StringUtils.isNotBlank(record.getStr12())) {
			update.set("str12", record.getStr12());
		}
		if (StringUtils.isNotBlank(record.getStr13())) {
			update.set("str13", record.getStr13());
		}
		if (StringUtils.isNotBlank(record.getStr14())) {
			update.set("str14", record.getStr14());
		}
		if (StringUtils.isNotBlank(record.getStr15())) {
			update.set("str15", record.getStr15());
		}
		if (StringUtils.isNotBlank(record.getStr16())) {
			update.set("str16", record.getStr16());
		}
		if (StringUtils.isNotBlank(record.getStr17())) {
			update.set("str17", record.getStr17());
		}
		if (StringUtils.isNotBlank(record.getStr19())) {
			update.set("str19", record.getStr19());
		}
		Class<?> entityClass = Record.class;
		WriteResult writeResult = mongoTemplate.updateFirst(query, update,
				entityClass);
		writeResult.getLastError();
		Record persitedRecord = mongoTemplate.findOne(query, Record.class);
		mongoTemplate.insert(Record_History
				.prepareNewHistoryRecord(persitedRecord));
	}

}
